package com.apache.passport.service.impl;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.passport.entity.LoginInfo;
import com.apache.passport.manager.LoginInfoManager;

import java.util.List;
import java.util.Map;

/**
 * description:  登录信息记录
 *
 * @author Hou Dayu 创建时间：2016-12-18
 */
public class LoginInfoManagerImpl implements LoginInfoManager {

    protected IDao loginInfoDao;

    protected final String entityName = "com.apache.passport.entity.LoginInfo";

    protected Map<String, PluginConnector> plugins;

    public void setPlugins(Map<String, PluginConnector> plugins) {
        this.plugins = plugins;
    }

    public void setLoginInfoDao(IDao loginInfoDao) {
        this.loginInfoDao = loginInfoDao;
    }

    public String saveInfo(ParamsVo<LoginInfo> vo) throws BusinessException {
        LoginInfo loginInfo = vo.getObj();
        //定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
        String cacheKey = "";
        //String cacheKey="loginInfo_"+infoId
        MethodParam param = new MethodParam("LoginInfo", cacheKey, "", entityName);
        param.setVaule(loginInfo);
        if (loginInfoDao.insert(param)) {
            return loginInfo.getUserEname();
        }

        return "";
    }

    public boolean editInfo(ParamsVo<LoginInfo> vo) throws BusinessException {
        LoginInfo loginInfo = vo.getObj();
        if (Validator.isNotNull(loginInfo.getUserEname())) {
            String cacheKey = "";
            //String cacheKey="loginInfo_"+loginInfo.getLgnInfoid()
            MethodParam param = new MethodParam("LoginInfo", cacheKey, "", entityName);
            param.setVaule(loginInfo);
            return loginInfoDao.edit(param);
        }
        return false;
    }

    public boolean deleteInfo(ParamsVo<LoginInfo> vo) throws BusinessException {
        String infoId = vo.getInfoId();
        if (Validator.isNull(infoId)) {
            return false;
        }
        String cacheKey = "";
        //String cacheKey="loginInfo_"+infoId
        String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        LoginInfo info = (LoginInfo) loginInfoDao.selectById(param);
        if (Validator.isEmpty(info)) {
            return false;
        }
        param.setVaule(info);//此处需要先将状态值赋值为删除状态
        if ("false".equals(mark)) {//逻辑删除
            param.setKey("LoginInfo");
            return loginInfoDao.edit(param);
        } else {
            param.setParams("lgnInfoid", infoId);
            param.setDelete(true);
            return loginInfoDao.delete(param);
        }
    }

    public Object getInfoById(ParamsVo<LoginInfo> vo) {
        String infoId = vo.getInfoId();
        String cacheKey = "";
        //String cacheKey="loginInfo_"+infoId
        if (Validator.isNull(infoId)) {
            return null;
        }
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        return loginInfoDao.selectById(param);
    }

    public Object execute(ParamsVo<LoginInfo> vo) {
        String key = vo.getMethodKey();
        try {
            if (!Validator.isEmpty(plugins)) {
                if (!Validator.isEmpty(plugins.get(key))) {
                    return plugins.get(key).execute(vo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Page getPageInfo(ParamsVo<LoginInfo> vo) {
        MethodParam param = setMethodParams(vo, 2);
        int pageSize = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
        int pageIndex = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
        param.setPageIndex(pageIndex);
        param.setPageSize(pageSize);
        Page page = loginInfoDao.pageSelect(param);
        return page;
    }

    public List<LoginInfo> getList(ParamsVo<LoginInfo> vo) {
        MethodParam param = setMethodParams(vo, 2);
        return loginInfoDao.select(param);
    }

    public long countInfo(ParamsVo<LoginInfo> vo) {
        MethodParam param = setMethodParams(vo, 1);
        return loginInfoDao.count(param);
    }

    private MethodParam setMethodParams(ParamsVo<LoginInfo> vo, int type) {
        String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
        StringBuilder sb = new StringBuilder(loginInfoDao.getSql(type));
        MethodParam param = new MethodParam(methodKey, "", "", entityName);
        LoginInfo loginInfo = vo.getObj();
        if (Validator.isNotNull(loginInfo.getLgnLastdevice())) {
            sb.append(" and lgnLastdevice = :lgnLastdevice");
            param.setParams("lgnLastdevice", loginInfo.getLgnLastdevice());
        }
        if (Validator.isNotNull(loginInfo.getSysEname())) {
            sb.append(" and sysEname = :sysEname");
            param.setParams("sysEname", loginInfo.getSysEname());
        }
        if (Validator.isNotNull(loginInfo.getLgnLasttime())) {
            sb.append(" and lgnLasttime = :lgnLasttime");
            param.setParams("lgnLasttime", loginInfo.getLgnLasttime());
        }
        if (Validator.isNotNull(loginInfo.getLgnLastsys())) {
            sb.append(" and lgnLastsys = :lgnLastsys");
            param.setParams("lgnLastsys", loginInfo.getLgnLastsys());
        }
        if (Validator.isNotNull(loginInfo.getLgnLastipaddress())) {
            sb.append(" and lgnLastipaddress = :lgnLastipaddress");
            param.setParams("lgnLastipaddress", loginInfo.getLgnLastipaddress());
        }
        if (Validator.isNotNull(loginInfo.getLgnLastarea())) {
            sb.append(" and lgnLastarea = :lgnLastarea");
            param.setParams("lgnLastarea", loginInfo.getLgnLastarea());
        }
        if (Validator.isNotNull(loginInfo.getUserEname())) {
            sb.append(" and userEname = :userEname");
            param.setParams("userEname", loginInfo.getUserEname());
        }
        if (Validator.isNotNull(loginInfo.getLgnJudge())) {
            sb.append(" and lgnJudge = :lgnJudge");
            param.setParams("lgnJudge", loginInfo.getLgnJudge());
        }

        param.setSqlStr(sb.toString());
        return param;
    }
}
